package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.HealthException;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Description: No Description
 * User: Eric
 */
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 获取登陆用户名
     */
    @GetMapping("/getLoginUsername")
    public Result getLoginUsername(){
        // 获取登陆用户的认证信息
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // 登陆用户名
        String username = loginUser.getUsername();
        // 返回给前端
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
    }
    @Reference
    private UserService userService;

    //添加用户
    @PostMapping("addUser")
    public Result addUser(@RequestBody com.itheima.health.pojo.User user, Integer[]roleIds){
        userService.addUser(user,roleIds);
        return new Result(true, "添加用户成功");
    }
    //通过用户id查出所有对应的角色id
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(int userId){
        List<Integer>list=userService.findRoleIdsByUserId(userId);
        return new Result(true,"查询id成功",list);
    }
    //分页查询
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<com.itheima.health.pojo.User> pageResult=userService.findPage(queryPageBean);
        List<com.itheima.health.pojo.User> users = pageResult.getRows();
        List<com.itheima.health.pojo.User>list=new ArrayList<>();
        for (com.itheima.health.pojo.User user : users) {
            try {
                String station = user.getStation();
                if (station.equals("0")){
                    list.add(user);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new HealthException("数据中有状态值station为空的，请联系管理员");
            }
        }
        pageResult.setRows(list);
        return new Result(true,"用户查询成功",pageResult);
    }
    //通过id查询用户
    @RequestMapping("findById")
    public Result findById(int userId){
        com.itheima.health.pojo.User user=userService.findById(userId);
        return new Result(true,"查询单个用户成功",user);
    }
    //通过id删除用户
    @RequestMapping("deleteUserById")
    public Result deleteUserById(int id){
        userService.deleteUserById(id);
        return new Result(true,"删除用户成功");
    }
    //更新用户
    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody com.itheima.health.pojo.User user, Integer[]roleIds){
        userService.updateUser(user,roleIds);
        return new Result(true,"更新用户成功");
    }
    //查询所有用户
    @RequestMapping("findUser")
    public Result findUser(){
        List<com.itheima.health.pojo.User>list=userService.findUser();
        return new Result(true,"查询所有用户成功",list);
    }
    @GetMapping("/getMenusByLoginUser")
    public Result getMenusByLoginUser(){
        // 获取登陆用户的认证信息
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // 登陆用户名
        String username = loginUser.getUsername();
        List<Menu> menuByLoadUserAuthority = userService.getMenuByLoadUserAuthority(username);
        return new Result(true,MessageConstant.GET_MENU_SUCCESS,menuByLoadUserAuthority);
    }
}
